package com.tool.permission.tag;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import org.apache.commons.lang3.StringUtils;

import com.tool.permission.subject.WebSubject;
import com.tool.permission.util.SecurityUtils;

public class HasPermissionTag extends TagSupport {

	private static final long serialVersionUID = -9030428706631385702L;
	/**
	 * 多个url用","分割
	 */
	private String url;
	
	@Override
	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest)this.pageContext.getRequest();
		WebSubject subject = SecurityUtils.getWebSubject(request);
		
		if(StringUtils.isEmpty(url)) {
			return SKIP_BODY;
		}
		
		String[] urls = url.split(",");
		
		for (String u : urls) {
			if(subject.isPermitted(url)) {
				return EVAL_BODY_INCLUDE;
			}
		}
		
		return SKIP_BODY;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}
	
}
